<?php
namespace BP\En;
require_once("java/math/BigDecimal.php");
require_once("BP/DA/DBAccess.php");
require_once("BP/DA/DBType.php");
require_once("BP/DA/DataType.php");
require_once("BP/DA/Paras.php");
require_once("BP/Sys/MapAttr.php");
require_once("BP/Sys/SysDocFile.php");
require_once("BP/Sys/SysEnums.php");
require_once("BP/Sys/SystemConfig.php");
require_once("BP/Tools/StringHelper.php");
class SqlBuilder {
	public static function GetKeyConditionOfMS ($en) // [Entity en]
	{
		$pk = $en->getPK();
		if ($pk->equals("OID"))
			return ($en->getEnMap()->getPhysicsTable() . ".OID=:OID");
		if ($pk->equals("No"))
			return ($en->getEnMap()->getPhysicsTable() . ".No=:No");
		if ($pk->equals("MyPK"))
			return ($en->getEnMap()->getPhysicsTable() . ".MyPK=:MyPK");
		if ($pk->equals("NodeID"))
			return ($en->getEnMap()->getPhysicsTable() . ".NodeID=:NodeID");
		if ($pk->equals("WorkID"))
			return ($en->getEnMap()->getPhysicsTable() . ".WorkID=:WorkID");
		$sql = " (1=1) ";
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$physicsTable = $enMap->getPhysicsTable();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$val = $en->GetValByKey($attr->getKey());
					if ((($val . NULL) . $val->equals("")))
					{
						throw new \RuntimeException((((((("@在执行[" . $en->getEnMap()->getEnDesc()) . " ") . $physicsTable) . "]没有给PK ") . $attr->getKey()) . " 赋�??,不能进行查询操作�?"));
					}
					$sql = (((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=:") . $attr->getKey());
					continue;
				}
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
			/* match: String */
					if ((($en->GetValIntByKey_String($attr->getKey()) == 0) && $attr->getKey()->equals("OID")))
					{
						throw new \RuntimeException((((((("@在执行[" . $en->getEnMap()->getEnDesc()) . " ") . $physicsTable) . "]，没有给PK ") . $attr->getKey()) . " 赋�??,不能进行查询操作�?"));
					}
					$sql = (((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=:") . $attr->getKey());
					continue;
				}
			}
		}
		return $sql;
	}
	public static function GetKeyConditionOfOLE ($en) // [Entity en]
	{
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		if ($attrs->Contains("OID"))
		{
			/* match: String */
			$key = $en->GetValIntByKey_String("OID");
			if (($key == 0))
			{
				throw new \RuntimeException((((("@在执行[" . $enMap->getEnDesc()) . " ") . $enMap->getPhysicsTable()) . "]，没有给PK OID 赋�??,不能进行查询操作�?"));
			}
			if ((count($en->getPKs()) /*from: en.getPKs().length*/ == 1))
			{
				return (($enMap->getPhysicsTable() . ".OID=") . $key);
			}
		}
		$sql = " (1=1) ";
		$physicsTable = $enMap->getPhysicsTable();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$val = $en->GetValByKey($attr->getKey());
					if ((($val . NULL) . $val->equals("")))
					{
						throw new \RuntimeException((((((("@在执行[" . $enMap->getEnDesc()) . " ") . $physicsTable) . "]没有给PK ") . $attr->getKey()) . " 赋�??,不能进行查询操作�?"));
					}
					$sql = ((((((($sql . " AND ") . $physicsTable) . ".[") . $attr->getField()) . "]='") . $en->GetValByKey($attr->getKey())) . "'");
					continue;
				}
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
			/* match: String */
					if ((($en->GetValIntByKey_String($attr->getKey()) == 0) && $attr->getKey()->equals("OID")))
					{
						throw new \RuntimeException((((((("@在执行[" . $enMap->getEnDesc()) . " ") . $physicsTable) . "]，没有给PK ") . $attr->getKey()) . " 赋�??,不能进行查询操作�?"));
					}
			/* match: String */
					$sql = (((((($sql . " AND ") . $physicsTable) . ".[") . $attr->getField()) . "]=") . $en->GetValStringByKey_String($attr->getKey()));
					continue;
				}
			}
		}
		return $sql;
	}
	public static function GenerWhereByPK ($en, $dbStr) // [Entity en, String dbStr]
	{
		if (($en->getPKCount() == 1))
		{
			if ($dbStr->equals("?"))
			{
				return (((($en->getEnMap()->getPhysicsTable() . ".") . $en->getPK()) . "=") . $dbStr);
			}
			else
			{
				return ((((($en->getEnMap()->getPhysicsTable() . ".") . $en->getPK()) . "=") . $dbStr) . $en->getPK());
			}
		}
		$sql = " (1=1) ";
		$attrs = $en->getEnMap()->getAttrs();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if ($dbStr->equals("?"))
				{
					$sql = (((($sql . " AND ") . $attr->getField()) . "=") . $dbStr);
				}
				else
				{
					$sql = ((((($sql . " AND ") . $attr->getField()) . "=") . $dbStr) . $attr->getField());
				}
			}
		}
		return $sql;
	}
	public static function GenerParasPK ($en) // [Entity en]
	{
		$paras = new Paras();
		$pk = $en->getPK();
		if ($pk->equals("OID"))
		{
			/* match: String */
			$paras->Add("OID", $en->GetValIntByKey_String("OID"));
			return $paras;
		}
		if ($pk->equals("No"))
		{
			/* match: String */
			$paras->Add("No", $en->GetValStrByKey_String("No"));
			return $paras;
		}
		if ($pk->equals("MyPK"))
		{
			/* match: String */
			$paras->Add("MyPK", $en->GetValStrByKey_String("MyPK"));
			return $paras;
		}
		if ($pk->equals("NodeID"))
		{
			/* match: String */
			$paras->Add("NodeID", $en->GetValIntByKey_String("NodeID"));
			return $paras;
		}
		if ($pk->equals("WorkID"))
		{
			/* match: String */
			$paras->Add("WorkID", $en->GetValIntByKey_String("WorkID"));
			return $paras;
		}
		$attrs = $en->getEnMap()->getAttrs();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$paras->Add($attr->getKey(), $en->GetValByKey($attr->getKey()));
					continue;
				}
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
			/* match: String */
					$paras->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
					continue;
				}
			}
		}
		return $paras;
	}
	public static function GetKeyConditionOfOraForPara ($en) // [Entity en]
	{
		$pk = $en->getPK();
		if ($pk->equals("OID"))
			return ($en->getEnMap()->getPhysicsTable() . ".OID=:OID");
		if ($pk->equals("No"))
			return ($en->getEnMap()->getPhysicsTable() . ".No=:No");
		if ($pk->equals("MyPK"))
			return ($en->getEnMap()->getPhysicsTable() . ".MyPK=:MyPK");
		if ($pk->equals("NodeID"))
			return ($en->getEnMap()->getPhysicsTable() . ".NodeID=:NodeID");
		if ($pk->equals("WorkID"))
			return ($en->getEnMap()->getPhysicsTable() . ".WorkID=:WorkID");
		$sql = " (1=1) ";
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$physicsTable = $enMap->getPhysicsTable();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$sql = ((((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=") . $en->getHisDBVarStr()) . $attr->getKey());
					continue;
				}
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
					$sql = ((((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=") . $en->getHisDBVarStr()) . $attr->getKey());
					continue;
				}
			}
		}
		return $sql->substring((new String(" (1=1)  AND "))->length());
	}
	public static function GetKeyConditionOfInformixForPara ($en) // [Entity en]
	{
		$pk = $en->getPK();
		if ($pk->equals("OID"))
			return ($en->getEnMap()->getPhysicsTable() . ".OID=:OID");
		if ($pk->equals("No"))
			return ($en->getEnMap()->getPhysicsTable() . ".No=:No");
		if ($pk->equals("MyPK"))
			return ($en->getEnMap()->getPhysicsTable() . ".MyPK=:MyPK");
		if ($pk->equals("NodeID"))
			return ($en->getEnMap()->getPhysicsTable() . ".NodeID=:NodeID");
		if ($pk->equals("WorkID"))
			return ($en->getEnMap()->getPhysicsTable() . ".WorkID=:WorkID");
		$sql = " (1=1) ";
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$physicsTable = $enMap->getPhysicsTable();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$sql = ((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=?");
					continue;
				}
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
					$sql = ((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=?");
					continue;
				}
			}
		}
		return $sql->substring((new String(" (1=1)  AND "))->length());
	}
	public static function GetKeyConditionOfMySQL ($en) // [Entity en]
	{
		$pk = $en->getPK();
		if ($pk->equals("OID"))
			return ($en->getEnMap()->getPhysicsTable() . ".OID=:OID");
		if ($pk->equals("No"))
			return ($en->getEnMap()->getPhysicsTable() . ".No=:No");
		if ($pk->equals("MyPK"))
			return ($en->getEnMap()->getPhysicsTable() . ".MyPK=:MyPK");
		if ($pk->equals("NodeID"))
			return ($en->getEnMap()->getPhysicsTable() . ".NodeID=:NodeID");
		if ($pk->equals("WorkID"))
			return ($en->getEnMap()->getPhysicsTable() . ".WorkID=:WorkID");
		$sql = " (1=1) ";
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$physicsTable = $enMap->getPhysicsTable();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$PK) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$sql = (((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=:") . $attr->getField());
					continue;
				}
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
					$sql = (((((($sql . " AND ") . $physicsTable) . ".") . $attr->getField()) . "=:") . $attr->getField());
					continue;
				}
			}
		}
		return $sql->substring((new String(" (1=1)  AND "))->length());
	}
	public static function RetrieveAll ($en) // [Entity en]
	{
		return SqlBuilder::SelectSQL($en, $SystemConfig->getTopNum());
	}
	public static function Retrieve ($en) // [Entity en]
	{
		$sql = "";
		switch ($SystemConfig->getAppCenterDBType()) {
			case $MSSQL:
			case $MySQL:
			/* match: Entity_I */
				$sql = (((SqlBuilder->SelectSQLOfMS_Entity_I($en, 1) . "   AND ( ") . SqlBuilder::GenerWhereByPK($en, ":")) . " )");
				break;
			case $Access:
				$sql = (((SqlBuilder::SelectSQLOfOLE($en, 1) . "  AND ( ") . SqlBuilder::GenerWhereByPK($en, ":")) . " )");
				break;
			case $Oracle:
			case $Informix:
				$sql = (((SqlBuilder->SelectSQLOfOra_Entity_I($en, 1) . "  AND ( ") . SqlBuilder::GenerWhereByPK($en, ":")) . " )");
				break;
			case $DB2:
				throw new \RuntimeException("还没有实现�??");
			default:
				throw new \RuntimeException("还没有实现�??");
		}
		$sql = $sql->replace("WHERE   AND", " WHERE ");
		$sql = $sql->replace("WHERE  AND", " WHERE ");
		$sql = $sql->replace("WHERE AND", " WHERE ");
		return $sql;
	}
	public static function RetrieveForPara ($en) // [Entity en]
	{
		$sql = NULL;
		switch ($SystemConfig->getAppCenterDBType()) {
			case $MSSQL:
			/* match: Entity_I */
				$sql = ((SqlBuilder->SelectSQLOfMS_Entity_I($en, 1) . " AND ") . SqlBuilder::GenerWhereByPK($en, ":"));
				break;
			case $MySQL:
				$sql = ((SqlBuilder::SelectSQLOfMySQL($en, 1) . " AND ") . SqlBuilder::GenerWhereByPK($en, ":"));
				break;
			case $Oracle:
				$sql = (((SqlBuilder->SelectSQLOfOra_Entity_I($en, 1) . "AND (") . SqlBuilder::GenerWhereByPK($en, ":")) . " )");
				break;
			case $Informix:
				$sql = (((SqlBuilder::SelectSQLOfInformix($en, 1) . " WHERE (") . SqlBuilder::GenerWhereByPK($en, "?")) . " )");
				break;
			case $Access:
				$sql = ((SqlBuilder::SelectSQLOfOLE($en, 1) . " AND ") . SqlBuilder::GenerWhereByPK($en, ":"));
				break;
			case $DB2:
			default:
				throw new \RuntimeException("还没有实现�??");
		}
		$sql = $sql->replace("WHERE  AND", "WHERE");
		$sql = $sql->replace("WHERE AND", "WHERE");
		return $sql;
	}
	public static function GenerFormWhereOfOra_Entity ($en) // [Entity en]
	{
		$enMap = $en->getEnMap();
		$from = (" FROM " . $enMap->getPhysicsTable());
		if (($enMap->getHisFKAttrs()->size() == 0))
		{
			return ($from . " WHERE ");
		}
		$mytable = $enMap->getPhysicsTable();
		$from += ",";
		$fkAttrs = $enMap->getHisFKAttrs();
		foreach ($fkAttrs as $attr) 		{
			if ((($attr == NULL) || ($attr->getMyFieldType() == FieldType::$RefText)))
			{
				continue;
			}
			$fktable = $attr->getHisFKEn()->getEnMap()->getPhysicsTable();
			$fktable = (($fktable . " T") . $attr->getKey());
			$from += ($fktable . " ,");
		}
		$from = $from->substring(0, ($from->length() - 1));
		$where = " WHERE ";
		$isAddAnd =  TRUE ;
		foreach ($fkAttrs as $attr) 		{
			if ((($attr == NULL) || ($attr->getMyFieldType() == FieldType::$RefText)))
			{
				continue;
			}
			$en1 = $attr->getHisFKEn();
			$fktable = ("T" . $attr->getKey());
			if (!$isAddAnd)
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$where += (((((((("(  " . $mytable) . ".") . $attr->getKey()) . "=") . $fktable) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue())) . "  (+) )");
				}
				else
				{
					$where += (((((((("(  " . $mytable) . ".") . $attr->getKey()) . "=") . $fktable) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue())) . "  (+) )");
				}
				$isAddAnd =  TRUE ;
			}
			else
			{
				if (($attr->getMyDataType() == $DataType->AppString))
				{
					$where += ((((((((" AND (  " . $mytable) . ".") . $attr->getKey()) . "=") . $fktable) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue())) . "  (+) )");
				}
				else
				{
					$where += ((((((((" AND (  " . $mytable) . ".") . $attr->getKey()) . "=") . $fktable) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue())) . "  (+) )");
				}
			}
		}
		$where = $where->replace("WHERE  AND", "WHERE");
		$where = $where->replace("WHERE AND", "WHERE");
		return ($from . $where);
	}
	public static function GenerFormWhereOfInformix ($en) // [Entity en]
	{
		$enMap = $en->getEnMap();
		$from = (" FROM " . $enMap->getPhysicsTable());
		$mytable = $enMap->getPhysicsTable();
		$fkAttrs = $enMap->getHisFKAttrs();
		$where = "";
		$mapAttr = NULL;
		foreach ($fkAttrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			if (!$attr->getIsFK())
				continue;
			$mapAttr = $attr->getToMapAttr();
			if ((($mapAttr->getLGType() == FieldTypeS::$Normal) && ($mapAttr->getUIContralType() == UIContralType::$DDL)))
				continue;
			$fktable = $attr->getHisFKEn()->getEnMap()->getPhysicsTable();
			$refAttr = $attr->getHisFKEn()->getEnMap()->GetAttrByKey($attr->getUIRefKeyValue());
			try 
			{
				if (DBAccess->IsExitsObject($fktable))
				{
					$where += (((((((((((((((" LEFT JOIN " . $fktable) . "  ") . $fktable) . "_") . $attr->getKey()) . "  ON ") . $mytable) . ".") . $attr->getField()) . "=") . $fktable) . "_") . $attr->getField()) . ".") . $refAttr->getField());
				}
			}
			catch (Exception $e)
			{
				$e->printStackTrace();
			}
		}
		$where = $where->replace("WHERE  AND", "WHERE");
		$where = $where->replace("WHERE AND", "WHERE");
		return ($from . $where);
	}
	public static function GenerCreateTableSQLOfMS ($en) // [Entity en]
	{
		if ((($en->getEnMap()->getPhysicsTable() . NULL) . $en->getEnMap()->getPhysicsTable()->equals("")))
		{
			return "DELETE FROM Sys_enum where enumkey='sdsf44a23'";
		}
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$sql = (("CREATE TABLE  " . $enMap->getPhysicsTable()) . " ( ");
		if (($attrs->size() == 0))
		{
			throw new \RuntimeException((("@" . $en->getEnDesc()) . " , 没有属�?�集�? attrs.size() = 0 ,能执行数据表的创�?."));
		}
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					if ($attr->getIsPK())
					{
						$sql += (((("[" . $attr->getField()) . "]  NVARCHAR (") . $attr->getMaxLength()) . ") NOT NULL,");
					}
					else
					{
						$sql += (((("[" . $attr->getField()) . "]  NVARCHAR (") . $attr->getMaxLength()) . ") NULL,");
					}
					break;
				case $DataType->AppRate:
				case $DataType->AppFloat:
				case $DataType->AppMoney:
					$sql += (("[" . $attr->getField()) . "] FLOAT NULL,");
					break;
				case $DataType->AppBoolean:
				case $DataType->AppInt:
					if ($attr->getIsPK())
					{
						try 
						{
							if ($attr->getUIBindKey()->equals("1"))
							{
								$sql += (("[" . $attr->getField()) . "] INT  primary key identity(1,1),");
							}
							else
							{
								$sql += (("[" . $attr->getField()) . "] INT NOT NULL,");
							}
						}
						catch (Exception $e)
						{
							$sql += (("[" . $attr->getField()) . "] INT NOT NULL,");
						}
					}
					else
					{
						$sql += (("[" . $attr->getField()) . "] INT  NULL,");
					}
					break;
				case $DataType->AppDouble:
					$sql += (("[" . $attr->getField()) . "]  FLOAT  NULL,");
					break;
				default:
					break;
			}
		}
		$sql = $sql->substring(0, ($sql->length() - 1));
		$sql += ")";
		return $sql;
	}
	public static function GenerCreateTableSQL ($en) // [Entity en]
	{
		switch (DBAccess->getAppCenterDBType()) {
			case $Oracle:
				return SqlBuilder::GenerCreateTableSQLOfOra_OK($en);
			case $Informix:
				return SqlBuilder::GenerCreateTableSQLOfInfoMix($en);
			case $MSSQL:
			case $Access:
				return SqlBuilder::GenerCreateTableSQLOfMS($en);
			default:
				break;
		}
		return NULL;
	}
	public static function DeleteSysEnumsSQL ($table, $key) // [String table, String key]
	{
		switch (DBAccess->getAppCenterDBType()) {
			case $Oracle:
				return (((("DELETE FROM " . $table) . " WHERE ") . $key) . "=:p");
			case $MySQL:
				return (((("DELETE FROM " . $table) . " WHERE ") . $key) . "=:p");
			case $MSSQL:
				return (((("DELETE FROM " . $table) . " WHERE ") . $key) . "=:p");
			case $Access:
				return (((("DELETE FROM " . $table) . " WHERE ") . $key) . "=:p");
			default:
				break;
		}
		return NULL;
	}
	public static function GenerCreateTableSQLOfOra_OK ($en) // [Entity en]
	{
		if (($en->getEnMap()->getPhysicsTable() . NULL))
		{
			throw new \RuntimeException((("您没有为[" . $en->getEnDesc()) . "],设置物理表�??"));
		}
		if (($en->getEnMap()->getPhysicsTable()->trim()->length() == 0))
		{
			throw new \RuntimeException((("您没有为[" . $en->getEnDesc()) . "],设置物理表�??"));
		}
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$sql = (("CREATE TABLE  " . $enMap->getPhysicsTable()) . " (");
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					if ($attr->getIsPK())
					{
						$sql += ((($attr->getField() . " varchar (") . $attr->getMaxLength()) . ") NOT NULL,");
					}
					else
					{
						$sql += ((($attr->getField() . " varchar (") . $attr->getMaxLength()) . ") NULL,");
					}
					break;
				case $DataType->AppRate:
				case $DataType->AppFloat:
				case $DataType->AppMoney:
				case $DataType->AppDouble:
					$sql += ($attr->getField() . " float NULL,");
					break;
				case $DataType->AppBoolean:
				case $DataType->AppInt:
					if ($attr->getIsPK())
					{
						if ("1" /* from: "1".equals(attr.getUIBindKey()) */)
						{
							$sql += ($attr->getField() . " int  primary key identity(1,1),");
						}
						else
						{
							$sql += ($attr->getField() . " int NOT NULL,");
						}
					}
					else
					{
						$sql += ($attr->getField() . " int ,");
					}
					break;
				default:
					break;
			}
		}
		$sql = $sql->substring(0, ($sql->length() - 1));
		$sql += ")";
		return $sql;
	}
	public static function GenerCreateTableSQLOfInfoMix ($en) // [Entity en]
	{
		if (($en->getEnMap()->getPhysicsTable() . NULL))
		{
			throw new \RuntimeException((("您没有为[" . $en->getEnDesc()) . "],设置物理表�??"));
		}
		if (($en->getEnMap()->getPhysicsTable()->trim()->length() == 0))
		{
			throw new \RuntimeException((("您没有为[" . $en->getEnDesc()) . "],设置物理表�??"));
		}
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$sql = (("CREATE TABLE  " . $enMap->getPhysicsTable()) . " (");
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					if (($attr->getMaxLength() >= 255))
					{
						if ($attr->getIsPK())
						{
							$sql += ((($attr->getField() . " lvarchar (") . $attr->getMaxLength()) . "),");
						}
						else
						{
							$sql += ((($attr->getField() . " lvarchar (") . $attr->getMaxLength()) . "),");
						}
					}
					else
					{
						if ($attr->getIsPK())
						{
							$sql += ((($attr->getField() . " varchar (") . $attr->getMaxLength()) . ") NOT NULL,");
						}
						else
						{
							$sql += ((($attr->getField() . " varchar (") . $attr->getMaxLength()) . "),");
						}
					}
					break;
				case $DataType->AppRate:
				case $DataType->AppFloat:
				case $DataType->AppMoney:
				case $DataType->AppDouble:
					$sql += ($attr->getField() . " float,");
					break;
				case $DataType->AppBoolean:
				case $DataType->AppInt:
					if ($attr->getIsPK())
					{
						if ($attr->getUIBindKey()->equals("1"))
						{
							$sql += ($attr->getField() . "  Serial not null,");
						}
						else
						{
							$sql += ($attr->getField() . " int8 NOT NULL,");
						}
					}
					else
					{
						$sql += ($attr->getField() . " int8,");
					}
					break;
				default:
					break;
			}
		}
		$sql = $sql->substring(0, ($sql->length() - 1));
		$sql += ")";
		return $sql;
	}
	public static function GenerCreateTableSQLOfMySQL ($en) // [Entity en]
	{
		if (($en->getEnMap()->getPhysicsTable() . NULL))
		{
			throw new \RuntimeException((("您没有为[" . $en->getEnDesc()) . "],设置物理表�??"));
		}
		if (($en->getEnMap()->getPhysicsTable()->trim()->length() == 0))
		{
			throw new \RuntimeException((((("您没有为[" . $en->getEnDesc()) . ", ") . $en) . "],设置物理表�??"));
		}
		$enMap = $en->getEnMap();
		$attrs = $enMap->getAttrs();
		$sql = (("CREATE TABLE  " . $enMap->getPhysicsTable()) . " (");
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					if ($attr->getIsPK())
					{
						$sql += ((($attr->getField() . " NVARCHAR (") . $attr->getMaxLength()) . ") NOT NULL,");
					}
					else
					{
						if (($attr->getMaxLength() > 3000))
						{
							$sql += ($attr->getField() . " TEXT NULL,");
						}
						else
						{
							$sql += ((($attr->getField() . " NVARCHAR (") . $attr->getMaxLength()) . ") NULL,");
						}
					}
					break;
				case $DataType->AppRate:
				case $DataType->AppFloat:
					$sql += ((($attr->getField() . " float  NULL COMMENT '") . $attr->getDesc()) . "',");
					break;
				case $DataType->AppMoney:
					$sql += ((($attr->getField() . " decimal(20,4)  NULL COMMENT '") . $attr->getDesc()) . "',");
					break;
				case $DataType->AppDouble:
					$sql += ((($attr->getField() . " double  NULL COMMENT '") . $attr->getDesc()) . "',");
					break;
				case $DataType->AppBoolean:
				case $DataType->AppInt:
					if ($attr->getIsPK())
					{
						if ("1" /* from: "1".equals(attr.getUIBindKey()) */)
						{
							$sql += ($attr->getField() . " int(4) primary key not null auto_increment,");
						}
						else
						{
							$sql += ($attr->getField() . " int   NULL,");
						}
					}
					else
					{
						$sql += ($attr->getField() . " int ,");
					}
					break;
				default:
					break;
			}
		}
		$sql = $sql->substring(0, ($sql->length() - 1));
		$sql += ") default charset=utf8 ";
		return $sql;
	}
	public static function GenerFormWhereOfOra_Entity_Map ($en, $map) // [Entity en, Map map]
	{
		$from = (" FROM " . $map->getPhysicsTable());
		$table = "";
		$tableAttr = "";
		$enTable = $map->getPhysicsTable();
		foreach ($map->getAttrs() as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$Normal) || ($attr->getMyFieldType() == FieldType::$PK)) || ($attr->getMyFieldType() == FieldType::$RefText)))
			{
				continue;
			}
			if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
			{
				$en1 = $attr->getHisFKEn();
				$table = $en1->getEnMap()->getPhysicsTable();
				$tableAttr = (("T" . $attr->getKey()) . "");
				$from = (((((((((((($from . " LEFT OUTER JOIN ") . $table) . "   ") . $tableAttr) . " ON ") . $enTable) . ".") . $attr->getField()) . "=") . $tableAttr) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue()));
				continue;
			}
			if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				$tableAttr = ("Enum_" . $attr->getKey());
				$from = ((((((((((($from . " LEFT OUTER JOIN ( SELECT Lab, IntKey FROM Sys_Enum WHERE EnumKey='") . $attr->getUIBindKey()) . "' )  Enum_") . $attr->getKey()) . " ON ") . $enTable) . ".") . $attr->getField()) . "=") . $tableAttr) . ".IntKey ");
			}
		}
		return ($from . "  WHERE (1=1) ");
	}
	public static function GenerFormWhereOfMS_Entity ($en) // [Entity en]
	{
		$enMap = $en->getEnMap();
		$from = (" FROM " . $enMap->getPhysicsTable());
		if (($enMap->getHisFKAttrs()->size() == 0))
		{
			return ($from . " WHERE (1=1)");
		}
		$mytable = $enMap->getPhysicsTable();
		$fkAttrs = $enMap->getAttrs();
		$mapAttr = NULL;
		foreach ($fkAttrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			if (!$attr->getIsFK())
				continue;
			$mapAttr = $attr->getToMapAttr();
			if ((($mapAttr->getLGType() == FieldTypeS::$Normal) && ($mapAttr->getUIContralType() == UIContralType::$DDL)))
				continue;
			$fktable = $attr->getHisFKEn()->getEnMap()->getPhysicsTable();
			$refAttr = $attr->getHisFKEn()->getEnMap()->GetAttrByKey($attr->getUIRefKeyValue());
			$from += (((((((((((((((" LEFT JOIN " . $fktable) . " AS ") . $fktable) . "_") . $attr->getKey()) . " ON ") . $mytable) . ".") . $attr->getField()) . "=") . $fktable) . "_") . $attr->getField()) . ".") . $refAttr->getField());
		}
		return ($from . " WHERE (1=1) ");
	}
	public static function GenerFormWhereOfMS_Entity_Map ($en, $map) // [Entity en, Map map]
	{
		$from = (" FROM " . $map->getPhysicsTable());
		$table = "";
		$tableAttr = "";
		$enTable = $map->getPhysicsTable();
		foreach ($map->getAttrs() as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$Normal) || ($attr->getMyFieldType() == FieldType::$PK)) || ($attr->getMyFieldType() == FieldType::$RefText)))
			{
				continue;
			}
			if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
			{
				$en1 = $attr->getHisFKEn();
				$table = $en1->getEnMap()->getPhysicsTable();
				$tableAttr = (($en1->getEnMap()->getPhysicsTable() . "_") . $attr->getKey());
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
			/* match: String */
					$from = (((((((((((((($from . " LEFT OUTER JOIN ") . $table) . " AS ") . $tableAttr) . " ON ISNULL( ") . $enTable) . ".") . $attr->getField()) . ", ") . $en->GetValIntByKey_String($attr->getKey())) . ")=") . $tableAttr) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue()));
				}
				else
				{
					$from = (((((((((((((($from . " LEFT OUTER JOIN ") . $table) . " AS ") . $tableAttr) . " ON ISNULL( ") . $enTable) . ".") . $attr->getField()) . ", '") . $en->GetValByKey($attr->getKey())) . "')=") . $tableAttr) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue()));
				}
				continue;
			}
			if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				$tableAttr = ("Enum_" . $attr->getKey());
			/* match: String */
				$from = ((((((((((((($from . " LEFT OUTER JOIN ( SELECT Lab, IntKey FROM Sys_Enum WHERE EnumKey='") . $attr->getUIBindKey()) . "' )  Enum_") . $attr->getKey()) . " ON ISNULL( ") . $enTable) . ".") . $attr->getField()) . ", ") . $en->GetValIntByKey_String($attr->getKey())) . ")=") . $tableAttr) . ".IntKey ");
			}
		}
		return ($from . "  WHERE (1=1) ");
	}
	protected static function GenerFormWhereOfMSOLE ($en) // [Entity en]
	{
		$enMap = $en->getEnMap();
		$fromTop = $enMap->getPhysicsTable();
		$from = "";
		$table = "";
		$tableAttr = "";
		$enTable = $enMap->getPhysicsTable();
		$attrs = $enMap->getAttrs();
		foreach ($attrs as $attr) 		{
			if (((($attr->getMyFieldType() == FieldType::$Normal) || ($attr->getMyFieldType() == FieldType::$PK)) || ($attr->getMyFieldType() == FieldType::$RefText)))
			{
				continue;
			}
			$fromTop = ("(" . $fromTop);
			if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
			{
				$en1 = $attr->getHisFKEn();
				$table = $en1->getEnMap()->getPhysicsTable();
				$tableAttr = (($en1->getEnMap()->getPhysicsTable() . "_") . $attr->getKey());
				if (($attr->getMyDataType() == $DataType->AppInt))
				{
			/* match: String */
					$from = (((((((((((((((((($from . " LEFT OUTER JOIN ") . $table) . " AS ") . $tableAttr) . " ON IIf( ISNULL( ") . $enTable) . ".") . $attr->getField()) . "), ") . $en->GetValIntByKey_String($attr->getKey())) . " , ") . $enTable) . ".") . $attr->getField()) . " )=") . $tableAttr) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue()));
				}
				else
				{
			/* match: String */
					$from = (((((((((((((((((($from . " LEFT OUTER JOIN ") . $table) . " AS ") . $tableAttr) . " ON IIf( ISNULL( ") . $enTable) . ".") . $attr->getField()) . "), '") . $en->GetValStringByKey_String($attr->getKey())) . "', ") . $enTable) . ".") . $attr->getField()) . " )=") . $tableAttr) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyValue()));
				}
			}
			if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				$tableAttr = ("Enum_" . $attr->getKey());
			/* match: String */
				$from = ((((((((((((((((($from . " LEFT OUTER JOIN ( SELECT Lab, IntKey FROM Sys_Enum WHERE EnumKey='") . $attr->getUIBindKey()) . "' )  Enum_") . $attr->getKey()) . " ON IIf( ISNULL( ") . $enTable) . ".") . $attr->getField()) . "), ") . $en->GetValIntByKey_String($attr->getKey())) . ", ") . $enTable) . ".") . $attr->getField()) . ")=") . $tableAttr) . ".IntKey ");
			}
			$from = ($from . ")");
		}
		$fromTop = (" FROM " . $fromTop);
		return (($fromTop . $from) . "  WHERE (1=1) ");
	}
	protected static function SelectSQLOfOra_Entity_I ($en, $topNum) // [Entity en, int topNum]
	{
		$val = "";
		$mainTable = "";
		$enMap = $en->getEnMap();
		if (($enMap->getHisFKAttrs()->size() != 0))
		{
			$mainTable = ($enMap->getPhysicsTable() . ".");
		}
		$attrs = $enMap->getAttrs();
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					$tempVar = $attr->getDefaultVal();
					if ((($tempVar == NULL) || $tempVar->equals("")))
					{
						if ($attr->getIsKeyEqualField())
						{
							$val = ((($val . ",") . $mainTable) . $attr->getField());
						}
						else
						{
							$val = ((((($val . ",") . $mainTable) . $attr->getField()) . "") . $attr->getKey());
						}
					}
					else
					{
						$val = ((((((($val . ",NVL(") . $mainTable) . $attr->getField()) . ", '") . $attr->getDefaultVal()) . "') ") . $attr->getKey());
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((($val . ", T") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
					if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
					{
						$val = (($val . ",") .Cash->getCaseWhenSQL($mainTable, $attr->getKey(), $attr->getField(), $attr->getUIBindKey(), $Integer->parseInt($attr->getDefaultVal()), $attr->UITag));
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((($val . ", T") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . "  AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppFloat:
				case $DataType->AppBoolean:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
					break;
				default:
					throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
			}
		}
			/* match: Entity */
		return ((" SELECT  " . $val->substring(1)) . SqlBuilder->GenerFormWhereOfOra_Entity($en));
	}
	protected static function SelectSQLOfInformix ($en, $topNum) // [Entity en, int topNum]
	{
		$val = "";
		$mainTable = "";
		if (($en->getEnMap()->getHisFKAttrs()->size() != 0))
		{
			$mainTable = ($en->getEnMap()->getPhysicsTable() . ".");
		}
		foreach ($en->getEnMap()->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					$tempVar = $attr->getDefaultVal();
					if (((($attr->getDefaultVal() == NULL) || ($tempVar == NULL)) || $tempVar->equals("")))
					{
						if ($attr->getIsKeyEqualField())
						{
							$val = ((($val . ", ") . $mainTable) . $attr->getField());
						}
						else
						{
							$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
						}
					}
					else
					{
						$val = ((((((($val . ",NVL(") . $mainTable) . $attr->getField()) . ", '") . $attr->getDefaultVal()) . "') ") . $attr->getKey());
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((((($val . ", ") . $map->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					$val = (((((((($val . ",NVL(") . $mainTable) . $attr->getField()) . ",") . $attr->getDefaultVal()) . ")   ") . $attr->getKey()) . "");
					if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
					{
						if ($StringHelper->isNullOrEmpty($attr->getUIBindKey()))
						{
							throw new \RuntimeException(((((("@" . $en) . " key=") . $attr->getKey()) . " UITag=") . $attr->UITag));
						}
						$ses = new SysEnums($attr->getUIBindKey(), $attr->UITag);
						$val = (($val . ",") . $ses->GenerCaseWhenForOracle($en, $mainTable, $attr->getKey(), $attr->getField(), $attr->getUIBindKey(), $Integer->parseInt($attr->getDefaultVal())));
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((((($val . ", ") . $map->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . "  AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppFloat:
					$val = ((((((($val . ", NVL( round(") . $mainTable) . $attr->getField()) . ",4) ,") . $attr->getDefaultVal()) . ") AS  ") . $attr->getKey());
					break;
				case $DataType->AppBoolean:
					if ($attr->getDefaultVal()->equals("0"))
					{
						$val = ((((($val . ", NVL( ") . $mainTable) . $attr->getField()) . ",0) ") . $attr->getKey());
					}
					else
					{
						$val = ((((($val . ", NVL(") . $mainTable) . $attr->getField()) . ",1) ") . $attr->getKey());
					}
					break;
				case $DataType->AppDouble:
					$val = ((((((($val . ", NVL( round(") . $mainTable) . $attr->getField()) . " ,4) ,") . $attr->getDefaultVal()) . ") ") . $attr->getKey());
					break;
				case $DataType->AppMoney:
					$val = ((((((($val . ", NVL( round(") . $mainTable) . $attr->getField()) . ",4),") . $attr->getDefaultVal()) . ") ") . $attr->getKey());
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					if ((($attr->getDefaultVal() == NULL) || $attr->getDefaultVal()->equals("")))
					{
						$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
					}
					else
					{
						$val = ((((((($val . ",NVL(") . $mainTable) . $attr->getField()) . ",'") . $attr->getDefaultVal()) . "') ") . $attr->getKey());
					}
					break;
				default:
					throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
			}
		}
		return ((" SELECT  " . $val->substring(1)) . SqlBuilder::GenerFormWhereOfInformix($en));
	}
	public static function SelectSQL ($en, $topNum) // [Entity en, int topNum]
	{
		switch ($SystemConfig->getAppCenterDBType()) {
			case $MSSQL:
			/* match: Entity_I */
				return SqlBuilder->SelectSQLOfMS_Entity_I($en, $topNum);
			case $MySQL:
				return SqlBuilder::SelectSQLOfMySQL($en, $topNum);
			case $Access:
				return SqlBuilder::SelectSQLOfOLE($en, $topNum);
			case $Oracle:
				return SqlBuilder->SelectSQLOfOra_Entity_I($en, $topNum);
			case $Informix:
				return SqlBuilder::SelectSQLOfInformix($en, $topNum);
			default:
				throw new \RuntimeException("没有判断的情�?");
		}
	}
	public static function SelectCountSQL ($en) // [Entity en]
	{
		switch ($SystemConfig->getAppCenterDBType()) {
			case $MSSQL:
				return SqlBuilder::SelectCountSQLOfMS($en);
			case $Access:
				return SqlBuilder::SelectSQLOfOLE($en, 0);
			case $Oracle:
			case $Informix:
				return SqlBuilder->SelectSQLOfOra_Entity_I($en, 0);
			default:
				return NULL;
		}
	}
	public static function SelectSQLOfOLE ($en, $topNum) // [Entity en, int topNum]
	{
		$val = "";
		$enMap = $en->getEnMap();
		$mainTable = ($enMap->getPhysicsTable() . ".");
		$attrs = $enMap->getAttrs();
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					$val = ((((((((((($val . ", IIf( ISNULL(") . $mainTable) . $attr->getField()) . "), '") . $attr->getDefaultVal()) . "', ") . $mainTable) . $attr->getField()) . " ) AS [") . $attr->getKey()) . "]");
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$en1 = $attr->getHisFKEn();
						$val = ((((((((((((((($val . ", IIf( ISNULL(") . $en1->getEnMap()->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . ") ,'',") . $en1->getEnMap()->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . ") AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					$val = ((((((((((($val . ",IIf( ISNULL(") . $mainTable) . $attr->getField()) . "),") . $attr->getDefaultVal()) . ",") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
					{
						$val = ((((((($val . ",IIf( ISNULL( Enum_") . $attr->getKey()) . ".Lab),'',Enum_") . $attr->getKey()) . ".Lab ) AS ") . $attr->getKey()) . "Text");
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$en1 = $attr->getHisFKEn();
						$val = ((((((((((((((($val . ", IIf( ISNULL(") . $en1->getEnMap()->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . "),0 ,") . $en1->getEnMap()->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . ") AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppFloat:
					$val = ((((((((((($val . ",IIf( ISNULL( Round(") . $mainTable) . $attr->getField()) . ",2) ),") . $attr->getDefaultVal()) . ",") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					break;
				case $DataType->AppBoolean:
					if ($attr->getDefaultVal()->equals("0"))
					{
						$val = ((((((((($val . ", IIf( ISNULL(") . $mainTable) . $attr->getField()) . "),0 ,") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					}
					else
					{
						$val = ((((((((($val . ",IIf( ISNULL(") . $mainTable) . $attr->getField()) . "),1,") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					}
					break;
				case $DataType->AppDouble:
					$val = ((((((((((($val . ", IIf(ISNULL( Round(") . $mainTable) . $attr->getField()) . ",4) ),") . $attr->getDefaultVal()) . ",") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					break;
				case $DataType->AppMoney:
					$val = ((((((((((($val . ",IIf( ISNULL(  Round(") . $mainTable) . $attr->getField()) . ",2) ),") . $attr->getDefaultVal()) . ",") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					break;
				case $DataType->AppDate:
					$val = ((((((((((($val . ", IIf(ISNULL( ") . $mainTable) . $attr->getField()) . "), '") . $attr->getDefaultVal()) . "',") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					break;
				case $DataType->AppDateTime:
					$val = ((((((((((($val . ", IIf(ISNULL(") . $mainTable) . $attr->getField()) . "), '") . $attr->getDefaultVal()) . "',") . $mainTable) . $attr->getField()) . ") AS [") . $attr->getKey()) . "]");
					break;
				default:
					throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
			}
		}
		if ((($topNum == -1) || ($topNum == 0)))
		{
			$topNum = 99999;
		}
		return ((((" SELECT TOP " . (new Integer($topNum))) . " ") . $val->substring(1)) . SqlBuilder::GenerFormWhereOfMSOLE($en));
	}
	public static function SelectSQLOfMS_Entity_I ($en, $topNum) // [Entity en, int topNum]
	{
		$val = "";
		$mainTable = "";
		$enMap = $en->getEnMap();
		if (($enMap->getHisFKAttrs()->size() != 0))
		{
			$mainTable = ($enMap->getPhysicsTable() . ".");
		}
		$atts = $enMap->getAttrs();
		foreach ($atts as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
				continue;
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					$tempVar = $attr->getDefaultVal();
					if (((($attr->getDefaultVal() == NULL) || ($tempVar == NULL)) || $tempVar->equals("")))
					{
						if ($attr->getIsKeyEqualField())
						{
							$val = ((($val . ", ") . $mainTable) . $attr->getField());
						}
						else
						{
							$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
						}
					}
					else
					{
						$val = ((((((($val . ",ISNULL(") . $mainTable) . $attr->getField()) . ", '") . $attr->getDefaultVal()) . "') ") . $attr->getKey());
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((((($val . ", ") . $map->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					$val = (((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey()) . "");
					if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
					{
						$val = (($val . ",") .Cash->getCaseWhenSQL($mainTable, $attr->getKey(), $attr->getField(), $attr->getUIBindKey(), $Integer->parseInt($attr->getDefaultVal()), $attr->UITag));
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						if (($attr->getHisFKEns() == NULL))
							throw new Exception((((((((("@生成SQL错误 Entity=" . $en) . " 外键字段�?") . $attr->getKey()) . ".") . $attr->getDesc()) . ", UIBindKey=") . $attr->getUIBindKey()) . "｝已经无�?, 也许该类或�?�外键字段被移除，请通知管理员解决�??"));
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((((($val . ", ") . $map->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . "  AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
				case $DataType->AppBoolean:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
					break;
				default:
					throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
			}
		}
		if ((($topNum == -1) || ($topNum == 0)))
		{
			$topNum = 99999;
		}
			/* match: Entity */
		return ((((" SELECT  TOP " . (new Integer($topNum))) . " ") . $val->substring(1)) . SqlBuilder->GenerFormWhereOfMS_Entity($en));
	}
	public static function SelectSQLOfMySQL ($en, $topNum) // [Entity en, int topNum]
	{
		$val = "";
		$mainTable = "";
		$enMap = $en->getEnMap();
		if (($enMap->getHisFKAttrs()->size() != 0))
		{
			$mainTable = ($enMap->getPhysicsTable() . ".");
		}
		$attrs = $enMap->getAttrs();
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
				continue;
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					$tempVar = $attr->getDefaultVal();
					if ((($tempVar == NULL) || $tempVar->equals("")))
					{
						if ($attr->getIsKeyEqualField())
						{
							$val = ((($val . ", ") . $mainTable) . $attr->getField());
						}
						else
						{
							$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
						}
					}
					else
					{
						$val = ((((((($val . ",IFNULL(") . $mainTable) . $attr->getField()) . ", '") . $attr->getDefaultVal()) . "') ") . $attr->getKey());
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((((($val . ", ") . $map->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
					if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
					{
						$val = (($val . ",") .Cash->getCaseWhenSQL($mainTable, $attr->getKey(), $attr->getField(), $attr->getUIBindKey(), $Integer->parseInt($attr->getDefaultVal()), $attr->UITag));
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						if (($attr->getHisFKEns() == NULL))
							throw new Exception((((((((("@生成SQL错误 Entity=" . $en) . " 外键字段�?") . $attr->getKey()) . ".") . $attr->getDesc()) . ", UIBindKey=") . $attr->getUIBindKey()) . "｝已经无�?, 也许该类或�?�外键字段被移除，请通知管理员解决�??"));
						$map = $attr->getHisFKEn()->getEnMap();
						$val = ((((((((($val . ", ") . $map->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $map->GetFieldByKey($attr->getUIRefKeyText())) . "  AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppFloat:
				case $DataType->AppBoolean:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " ") . $attr->getKey());
					break;
				default:
					throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
			}
		}
			/* match: Entity */
		return ((" SELECT   " . $val->substring(1)) . SqlBuilder->GenerFormWhereOfMS_Entity($en));
	}
	public static function SelectCountSQLOfMS ($en) // [Entity en]
	{
		$sql = "SELECT COUNT(*) ";
			/* match: Entity_Map */
		return ($sql . SqlBuilder->GenerFormWhereOfMS_Entity_Map($en, $en->getEnMap()));
	}
	public static function SelectSQLOfOra_String_Map ($enName, $map) // [String enName, Map map]
	{
		$val = "";
		$mainTable = ($map->getPhysicsTable() . ".");
		foreach ($map->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					$tempVar = $attr->getDefaultVal();
					if (((($attr->getDefaultVal() == NULL) || ($tempVar == NULL)) || $tempVar->equals("")))
					{
						if ($attr->getIsKeyEqualField())
						{
							$val = ((($val . ",") . $mainTable) . $attr->getField());
						}
						else
						{
							$val = ((((($val . ",") . $mainTable) . $attr->getField()) . "") . $attr->getKey());
						}
					}
					else
					{
						$val = ((((((($val . ",NVL(") . $mainTable) . $attr->getField()) . ", '") . $attr->getDefaultVal()) . "') ") . $attr->getKey());
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$en1 = $attr->getHisFKEn();
						$val = ((((((($val . ", T") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					$val = (((((((($val . ", NVL(") . $mainTable) . $attr->getField()) . ",") . $attr->getDefaultVal()) . ") AS  ") . $attr->getKey()) . "");
					if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
					{
						$val = (($val . ",") .Cash->getCaseWhenSQL($mainTable, $attr->getKey(), $attr->getField(), $attr->getUIBindKey(), $Integer->parseInt($attr->getDefaultVal()), $attr->UITag));
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$map1 = $attr->getHisFKEn()->getEnMap();
						$val = ((((((($val . ", T") . $attr->getKey()) . ".") . $map1->GetFieldByKey($attr->getUIRefKeyText())) . "  AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppFloat:
				case $DataType->AppBoolean:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					$val = ((((($val . ",") . $mainTable) . $attr->getField()) . " AS ") . $attr->getKey());
					break;
				default:
					throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
			}
		}
		return ("SELECT " . $val->substring(1));
	}
	public static function SelectSQLOfMS_Map ($map) // [Map map]
	{
		$val = "";
		$mainTable = ($map->getPhysicsTable() . ".");
		foreach ($map->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					if (($attr->getDefaultVal() == NULL))
					{
						$val = (((((($val . " ") . $mainTable) . $attr->getField()) . " AS [") . $attr->getKey()) . "]");
					}
					else
					{
						$val = (((((((($val . ",ISNULL(") . $mainTable) . $attr->getField()) . ", '") . $attr->getDefaultVal()) . "') AS [") . $attr->getKey()) . "]");
					}
					if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
					{
						$en1 = $attr->getHisFKEn();
						$val = ((((((((($val . ",") . $en1->getEnMap()->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
					}
					break;
				case $DataType->AppInt:
					if ($attr->getIsNull())
					{
						$val = (((((($val . ", ") . $mainTable) . $attr->getField()) . " AS [") . $attr->getKey()) . "]");
					}
					else
					{
						$val = (((((((($val . ", ISNULL(") . $mainTable) . $attr->getField()) . ",") . $attr->getDefaultVal()) . ") AS [") . $attr->getKey()) . "]");
						if ((($attr->getMyFieldType() == FieldType::$Enum) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
						{
							$val = ((((($val . ", Enum_") . $attr->getKey()) . ".Lab  AS ") . $attr->getKey()) . "Text");
						}
						if ((($attr->getMyFieldType() == FieldType::$FK) || ($attr->getMyFieldType() == FieldType::$PKFK)))
						{
							$en1 = $attr->getHisFKEn();
							$val = ((((((((($val . ", ") . $en1->getEnMap()->getPhysicsTable()) . "_") . $attr->getKey()) . ".") . $en1->getEnMap()->GetFieldByKey($attr->getUIRefKeyText())) . " AS ") . $attr->getKey()) . "Text");
						}
					}
					break;
				case $DataType->AppFloat:
				case $DataType->AppBoolean:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
				case $DataType->AppDate:
				case $DataType->AppDateTime:
					$val = (((((($val . ", ") . $mainTable) . $attr->getField()) . " AS [") . $attr->getKey()) . "]");
					break;
				default:
					if ($attr->getKey()->equals("MyNum"))
					{
						$val = ($val . ", COUNT(*)  AS MyNum ");
						break;
					}
					else
					{
						throw new \RuntimeException(((("@没有定义的数据类�?! attr=" . $attr->getKey()) . " MyDataType =") . $attr->getMyDataType()));
					}
			}
		}
		return ("SELECT TOP @TopNum " . $val->substring(1));
	}
	public static function UpdateOfMSAccess ($en, $keys) // [Entity en, String[] keys]
	{
		$val = "";
		$attrs = $en->getEnMap()->getAttrs();
		foreach ($attrs as $attr) 		{
			if (($keys != NULL))
			{
				$isHave =  FALSE ;
				foreach ($keys as $s) 				{
					if ($attr->getKey()->equals($s))
					{
						$isHave =  TRUE ;
						break;
					}
				}
				if (!$isHave)
				{
					continue;
				}
			}
			if ((((($attr->getMyFieldType() == FieldType::$RefText) || ($attr->getMyFieldType() == FieldType::$PK)) || ($attr->getMyFieldType() == FieldType::$PKFK)) || ($attr->getMyFieldType() == FieldType::$PKEnum)))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
			/* match: String */
					$val = ((((($val . ",[") . $attr->getField()) . "]='") . $en->GetValStringByKey_String($attr->getKey())) . "'");
					break;
				case $DataType->AppInt:
			/* match: String */
					$val = (((($val . ",[") . $attr->getField()) . "]=") . $en->GetValStringByKey_String($attr->getKey()));
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$str = $en->GetValStringByKey_String($attr->getKey());
					$str = $str->replace("�?", "");
					$str = $str->replace(",", "");
					$val = (((($val . ",[") . $attr->getField()) . "]=") . $str);
					break;
				case $DataType->AppBoolean:
			/* match: String */
					$val = (((($val . ",[") . $attr->getField()) . "]=") . $en->GetValStringByKey_String($attr->getKey()));
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					if (($da->indexOf("_DATE") == -1))
					{
						$val = ((((($val . ",[") . $attr->getField()) . "]='") . $da) . "'");
					}
					else
					{
						$val = (((($val . ",[") . $attr->getField()) . "]=") . $da);
					}
					break;
				default:
					throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
			}
		}
		$sql = "";
		if ($val->equals(""))
		{
			foreach ($attrs as $attr) 			{
				if (($keys != NULL))
				{
					$isHave =  FALSE ;
					foreach ($keys as $s) 					{
						if ($attr->getKey()->equals($s))
						{
							$isHave =  TRUE ;
							break;
						}
					}
					if (!$isHave)
					{
						continue;
					}
				}
				switch ($attr->getMyDataType()) {
					case $DataType->AppString:
			/* match: String */
						$val = ((((($val . ",[") . $attr->getField()) . "]='") . $en->GetValStringByKey_String($attr->getKey())) . "'");
						break;
					case $DataType->AppInt:
			/* match: String */
						$val = (((($val . ",[") . $attr->getField()) . "]=") . $en->GetValStringByKey_String($attr->getKey()));
						break;
					case $DataType->AppFloat:
					case $DataType->AppDouble:
					case $DataType->AppMoney:
			/* match: String */
						$str = $en->GetValStringByKey_String($attr->getKey());
						$str = $str->replace("�?", "");
						$str = $str->replace(",", "");
						$val = (((($val . ",[") . $attr->getField()) . "]=") . $str);
						break;
					case $DataType->AppBoolean:
			/* match: String */
						$val = (((($val . ",[") . $attr->getField()) . "]=") . $en->GetValStringByKey_String($attr->getKey()));
						break;
					case $DataType->AppDate:
					case $DataType->AppDateTime:
			/* match: String */
						$da = $en->GetValStringByKey_String($attr->getKey());
						if (($da->indexOf("_DATE") == -1))
						{
							$val = ((((($val . ",[") . $attr->getField()) . "]='") . $da) . "'");
						}
						else
						{
							$val = (((($val . ",[") . $attr->getField()) . "]=") . $da);
						}
						break;
					default:
						throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
				}
			}
		}
		if ($val->equals(""))
		{
			$ms = "";
			foreach ($keys as $str) 			{
				$ms += $str;
			}
			throw new \RuntimeException(((($en->getEnDesc() . "执行更新错误：无效的属�?�[") . $ms) . "]对于本实体来说�??"));
		}
		$sql = ((((("UPDATE " . $en->getEnMap()->getPhysicsTable()) . " SET ") . $val->substring(1)) . " WHERE ") . SqlBuilder::GetKeyConditionOfOLE($en));
		return $sql->replace(",=''", "");
	}
	public static function Update ($en, $keys) // [Entity en, String[] keys]
	{
		$map = $en->getEnMap();
		if (($map->getAttrs()->size() == 0))
		{
			throw new \RuntimeException((("@实体�?" . $en) . " ,Attrs属�?�集合信息丢失，导致无法生成SQL�?"));
		}
		$val = "";
		foreach ($map->getAttrs() as $attr) 		{
			if (($keys != NULL))
			{
				$isHave =  FALSE ;
				foreach ($keys as $s) 				{
					if ($attr->getKey()->equals($s))
					{
						$isHave =  TRUE ;
						break;
					}
				}
				if (!$isHave)
				{
					continue;
				}
			}
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					if (($map->getEnDBUrl()->getDBType() == $DBType->Access))
					{
			/* match: String */
						$val = ((((($val . ",[") . $attr->getField()) . "]='") . $en->GetValStringByKey_String($attr->getKey())->replace('\'', '~')) . "'");
					}
					else
					{
						if (($attr->getUIIsDoc() && $attr->getKey()->equals("Doc")))
						{
			/* match: String */
							$doc = $en->GetValStringByKey_String($attr->getKey());
							if ($map->getAttrs()->Contains("DocLength"))
							{
								$en->SetValByKey_String_F("DocLength", $doc->length());
							}
							if (($doc->length() >= 2000))
							{
								$SysDocFile->SetValV2($en, $en->getPKVal(), $doc);
								$val = ((($val . ",") . $attr->getField()) . "=''");
							}
							else
							{
			/* match: String */
								$val = ((((($val . ",") . $attr->getField()) . "='") . $en->GetValStringByKey_String($attr->getKey())->replace('\'', '~')) . "'");
							}
						}
						else
						{
			/* match: String */
							$val = ((((($val . ",") . $attr->getField()) . "='") . $en->GetValStringByKey_String($attr->getKey())->replace('\'', '~')) . "'");
						}
					}
					break;
				case $DataType->AppInt:
			/* match: String */
					$val = (((($val . ",") . $attr->getField()) . "=") . $en->GetValStringByKey_String($attr->getKey()));
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$str = $en->GetValStringByKey_String($attr->getKey());
					$str = $str->replace("�?", "");
					$str = $str->replace(",", "");
					$val = (((($val . ",") . $attr->getField()) . "=") . $str);
					break;
				case $DataType->AppBoolean:
			/* match: String */
					$val = (((($val . ",") . $attr->getField()) . "=") . $en->GetValStringByKey_String($attr->getKey()));
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					if (($da->indexOf("_DATE") == -1))
					{
						$val = ((((($val . ",") . $attr->getField()) . "='") . $da) . "'");
					}
					else
					{
						$val = (((($val . ",") . $attr->getField()) . "=") . $da);
					}
					break;
				default:
					throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
			}
		}
		$sql = "";
		if ($val->equals(""))
		{
			foreach ($map->getAttrs() as $attr) 			{
				if (($keys != NULL))
				{
					$isHave =  FALSE ;
					foreach ($keys as $s) 					{
						if ($attr->getKey()->equals($s))
						{
							$isHave =  TRUE ;
							break;
						}
					}
					if (!$isHave)
					{
						continue;
					}
				}
				switch ($attr->getMyDataType()) {
					case $DataType->AppString:
			/* match: String */
						$val = ((((($val . ",") . $attr->getField()) . "='") . $en->GetValStringByKey_String($attr->getKey())) . "'");
						break;
					case $DataType->AppInt:
			/* match: String */
						$val = (((($val . ",") . $attr->getField()) . "=") . $en->GetValStringByKey_String($attr->getKey()));
						break;
					case $DataType->AppFloat:
					case $DataType->AppDouble:
					case $DataType->AppMoney:
			/* match: String */
						$str = $en->GetValStringByKey_String($attr->getKey());
						$str = $str->replace("�?", "");
						$str = $str->replace(",", "");
						$val = (((($val . ",") . $attr->getField()) . "=") . $str);
						break;
					case $DataType->AppBoolean:
			/* match: String */
						$val = (((($val . ",") . $attr->getField()) . "=") . $en->GetValStringByKey_String($attr->getKey()));
						break;
					case $DataType->AppDate:
					case $DataType->AppDateTime:
			/* match: String */
						$da = $en->GetValStringByKey_String($attr->getKey());
						if (($da->indexOf("_DATE") == -1))
						{
							$val = ((((($val . ",") . $attr->getField()) . "='") . $da) . "'");
						}
						else
						{
							$val = (((($val . ",") . $attr->getField()) . "=") . $da);
						}
						break;
					default:
						throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
				}
			}
		}
		if ($val->equals(""))
		{
			$ms = "";
			foreach ($keys as $str) 			{
				$ms += $str;
			}
			throw new \RuntimeException(((($en->getEnDesc() . "执行更新错误：无效的属�?�[") . $ms) . "]对于本实体来说�??"));
		}
		switch ($SystemConfig->getAppCenterDBType()) {
			case $MSSQL:
			case $Access:
				$sql = ((((("UPDATE " . $en->getEnMap()->getPhysicsTable()) . " SET ") . $val->substring(1)) . " WHERE ") . SqlBuilder::GenerWhereByPK($en, ":"));
				break;
			case $MySQL:
				$sql = ((((("UPDATE " . $en->getEnMap()->getPhysicsTable()) . " SET ") . $val->substring(1)) . " WHERE ") . SqlBuilder::GenerWhereByPK($en, "?"));
				break;
			case $Oracle:
				$sql = ((((("UPDATE " . $en->getEnMap()->getPhysicsTable()) . " SET ") . $val->substring(1)) . " WHERE ") . SqlBuilder::GenerWhereByPK($en, ":"));
				break;
			case $Informix:
				$sql = ((((("UPDATE " . $en->getEnMap()->getPhysicsTable()) . " SET ") . $val->substring(1)) . " WHERE ") . SqlBuilder::GenerWhereByPK($en, ":"));
				break;
			default:
				throw new \RuntimeException("no this case db type . ");
		}
		return $sql->replace(",=''", "");
	}
	public static function GenerParas_Update_Informix_Entity_aString ($en, $keys) // [Entity en, String[] keys]
	{
		if (($keys == NULL))
		{
			/* match: Entity */
			return SqlBuilder->GenerParas_Update_Informix_Entity($en);
		}
		$mykeys = "@";
		foreach ($keys as $key) 		{
			$mykeys += ($key . "@");
		}
		$map = $en->getEnMap();
		$ps = new Paras();
		foreach ($map->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			if ($attr->getIsPK())
			{
				continue;
			}
			if (!$mykeys->contains((("@" . $attr->getKey()) . "@")))
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					if (($attr->getUIIsDoc() && $attr->getKey()->equals("Doc")))
					{
			/* match: String */
						$doc = $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~');
						if ($map->getAttrs()->Contains("DocLength"))
						{
							$en->SetValByKey_String_F("DocLength", $doc->length());
						}
						if (($doc->length() >= 2000))
						{
							$SysDocFile->SetValV2($en, $en->getPKVal(), $doc);
							$ps->Add($attr->getKey(), "");
						}
						else
						{
			/* match: String */
							$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
						}
					}
					else
					{
			/* match: String */
						$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
					}
					break;
				case $DataType->AppBoolean:
				case $DataType->AppInt:
			/* match: String */
					$ps->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$str = $en->GetValStrByKey_String($attr->getKey());
					$str = $str->replace("�?", "");
					$str = $str->replace(",", "");
					if (is_null($str) || $str == '')
					{
						$ps->Add($attr->getKey(), 0);
					}
					else
					{
						$ps->Add($attr->getKey(), new BigDecimal($str));
					}
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					$ps->Add($attr->getKey(), $da);
					break;
				default:
					throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
			}
		}
		if ((($en->getPK()->equals("OID") || $en->getPK()->equals("WorkID")) || $en->getPK()->equals("FID")))
		{
			/* match: String */
			$ps->Add($en->getPK(), $en->GetValIntByKey_String($en->getPK()));
		}
		else
		{
			/* match: String */
			$ps->Add($en->getPK(), $en->GetValStrByKey_String($en->getPK()));
		}
		return $ps;
	}
	public static function GenerParas_Update_Informix_Entity ($en) // [Entity en]
	{
		$map = $en->getEnMap();
		$ps = new Paras();
		foreach ($map->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			if ($attr->getIsPK())
			{
				continue;
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					if (($attr->getUIIsDoc() && $attr->getKey()->equals("Doc")))
					{
			/* match: String */
						$doc = $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~');
						if ($map->getAttrs()->Contains("DocLength"))
						{
							$en->SetValByKey_String_F("DocLength", $doc->length());
						}
						if (($doc->length() >= 2000))
						{
							$SysDocFile->SetValV2($en, $en->getPKVal(), $doc);
							$ps->Add($attr->getKey(), "");
						}
						else
						{
			/* match: String */
							$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
						}
					}
					else
					{
			/* match: String */
						$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
					}
					break;
				case $DataType->AppBoolean:
				case $DataType->AppInt:
			/* match: String */
					$ps->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$str = $en->GetValStrByKey_String($attr->getKey());
					$str = $str->replace("�?", "");
					$str = $str->replace(",", "");
					if (is_null($str) || $str == '')
					{
						$ps->Add($attr->getKey(), 0);
					}
					else
					{
						$ps->Add($attr->getKey(), new BigDecimal($str));
					}
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					$ps->Add($attr->getKey(), $da);
					break;
				default:
					throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
			}
		}
		$pk = $en->getPK();
		if (($pk->equals("OID") || $pk->equals("WorkID")))
		{
			/* match: String */
			$ps->Add($en->getPK(), $en->GetValIntByKey_String($pk));
		}
		else
			if ((($pk->equals("No") || $pk->equals("MyPK")) || $pk->equals("ID")))
			{
			/* match: String */
				$ps->Add($en->getPK(), $en->GetValStrByKey_String($pk));
			}
			else
			{
				foreach ($map->getAttrs() as $attr) 				{
					if (!$attr->getIsPK())
					{
						continue;
					}
					switch ($attr->getMyDataType()) {
						case $DataType->AppString:
			/* match: String */
							$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
							break;
						case $DataType->AppInt:
			/* match: String */
							$ps->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
							break;
						default:
							throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型...");
					}
				}
			}
		return $ps;
	}
	public static function GenerParas ($en, $keys) // [Entity en, String[] keys]
	{
		$IsEnableNull = SystemConfig::getIsEnableNull();
		$mykeys = "@";
		if (($keys != NULL))
		{
			foreach ($keys as $key) 			{
				$mykeys += ($key . "@");
			}
		}
		$map = $en->getEnMap();
		$ps = new Paras();
		foreach ($map->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			if (($keys != NULL))
			{
				if (!$mykeys->contains((("@" . $attr->getKey()) . "@")))
				{
					if (!$attr->getIsPK())
					{
						continue;
					}
				}
			}
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
					if (($attr->getUIIsDoc() && $attr->getKey()->equals("Doc")))
					{
			/* match: String */
						$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
					}
					else
					{
			/* match: String */
						$ps->Add($attr->getKey(), $en->GetValStrByKey_String($attr->getKey())->replace('\'', '~'));
					}
					break;
				case $DataType->AppBoolean:
			/* match: String */
					$ps->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
					break;
				case $DataType->AppInt:
					if ($attr->getKey()->equals("MyPK"))
					{
						$ps->Add($attr->getKey(), $en->GetValInt64ByKey($attr->getKey()));
					}
					else
					{
						if ($IsEnableNull)
						{
			/* match: String */
							$s = $en->GetValStrByKey_String($attr->getKey());
							if (is_null($s) || $s == '')
							{
								$ps->Add($attr->getKey(), 0);
							}
							else
							{
			/* match: String */
								$ps->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
							}
						}
						else
						{
			/* match: String */
							$ps->Add($attr->getKey(), $en->GetValIntByKey_String($attr->getKey()));
						}
					}
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$str = $en->GetValStrByKey_String($attr->getKey());
					$str = $str->replace("�?", "");
					$str = $str->replace(",", "");
					if (is_null($str) || $str == '')
					{
						if ($IsEnableNull)
						{
							$ps->Add($attr->getKey(), NULL, BigDecimal);
						}
						else
						{
							$ps->Add($attr->getKey(), 0);
						}
					}
					else
					{
						$ps->Add($attr->getKey(), new BigDecimal($str));
					}
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					$ps->Add($attr->getKey(), $da);
					break;
				default:
					throw new \RuntimeException("@SqlBulider.update, 没有这个数据类型");
			}
		}
		if (($keys != NULL))
		{
			if (($en->getPK()->equals("OID") || $en->getPK()->equals("WorkID")))
			{
			/* match: String */
				$ps->Add($en->getPK(), $en->GetValIntByKey_String($en->getPK()));
			}
			else
			{
			/* match: String */
				$ps->Add($en->getPK(), $en->GetValStrByKey_String($en->getPK()));
			}
		}
		return $ps;
	}
	public static function UpdateForPara ($en, $keys) // [Entity en, String[] keys]
	{
		$mykey = "";
		if (($keys != NULL))
		{
			foreach ($keys as $s) 			{
				$mykey += (("@" . $s) . ",");
			}
		}
		$dbVarStr = $en->getHisDBVarStr();
		$map = $en->getEnMap();
		$val = "";
		foreach ($map->getAttrs() as $attr) 		{
			if ((($attr->getMyFieldType() == FieldType::$RefText) || $attr->getIsPK()))
			{
				continue;
			}
			if (($keys != NULL))
			{
				if (!$mykey->contains((("@" . $attr->getKey()) . ",")))
				{
					continue;
				}
			}
			$val = ((((($val . ",") . $attr->getField()) . "=") . $dbVarStr) . $attr->getKey());
		}
		if (is_null($val) || $val == '')
		{
			foreach ($map->getAttrs() as $attr) 			{
				if (($attr->getMyFieldType() == FieldType::$RefText))
				{
					continue;
				}
				if (($keys != NULL))
				{
					if (!$mykey->contains((("@" . $attr->getKey()) . ",")))
					{
						continue;
					}
				}
				$val = (((($val . ",") . $attr->getField()) . "=") . $attr->getField());
			}
		}
		if (!(is_null($val) || $val == ''))
		{
			$val = $val->substring(1);
		}
		$sql = ((((("UPDATE " . $en->getEnMap()->getPhysicsTable()) . " SET ") . $val) . " WHERE ") . SqlBuilder::GenerWhereByPK($en, $en->getHisDBVarStr()));
		return $sql->replace(",=''", "");
	}
	public static function DeleteForPara ($en) // [Entity en]
	{
		$dbstr = $en->getHisDBVarStr();
		$sql = ((("DELETE FROM " . $en->getEnMap()->getPhysicsTable()) . " WHERE ") . SqlBuilder::GenerWhereByPK($en, $dbstr));
		return $sql;
	}
	public static function InsertForPara ($en) // [Entity en]
	{
		$dbstr = $en->getHisDBVarStr();
		$isMySQL =  FALSE ;
		if (($SystemConfig->getAppCenterDBType() == $DBType->MySQL))
			$isMySQL =  TRUE ;
		$field = ""$val = "";
		$attrs = $en->getEnMap()->getAttrs();
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
				continue;
			if ($isMySQL)
				$field = (($field . ",") . $attr->getField());
			else
				$field = ((($field . ",[") . $attr->getField()) . "]");
			$val = ((($val . ",") . $dbstr) . $attr->getKey());
		}
		$sql = (((((("INSERT INTO " . $en->getEnMap()->getPhysicsTable()) . " (") . $field->substring(1)) . " ) VALUES ( ") . $val->substring(1)) . ")");
		return $sql;
	}
	public static function InsertForPara_Informix ($en) // [Entity en]
	{
		$isInnkey =  FALSE ;
		if ($en->getIsOIDEntity())
		{
			$isInnkey =  FALSE ;
		}
		$field = ""$val = "";
		$attrs = $en->getEnMap()->getAttrs();
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			if ($isInnkey)
			{
				if ($attr->getKey()->equals("OID"))
				{
					continue;
				}
			}
			$field = ((($field . ",[") . $attr->getField()) . "]");
			$val = ($val . ",?");
		}
		$sql = (((((("INSERT INTO " . $en->getEnMap()->getPhysicsTable()) . " (") . $field->substring(1)) . " ) VALUES ( ") . $val->substring(1)) . ")");
		return $sql;
	}
	public static function Insert ($en) // [Entity en]
	{
		$key = ""$field = ""$val = "";
		$attrs = $en->getEnMap()->getAttrs();
		if (($attrs->size() == 0))
			throw new \RuntimeException((("@实体�?" . $en) . " ,Attrs属�?�集合信息丢失，导致无法生成SQL�?"));
		foreach ($attrs as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
				continue;
			$key = $attr->getKey();
			$field = (($field . ",") . $attr->getField());
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
			/* match: String */
					$str = $en->GetValStringByKey_String($key);
					if (is_null($str) || $str == '')
					{
						$str = "";
					}
					else
					{
						$str = $str->replace('\'', '~');
					}
					if (($attr->getUIIsDoc() && $attr->getKey()->equals("Doc")))
					{
						if ($attrs->Contains("DocLength"))
						{
							$en->SetValByKey_String_F("DocLength", $str->length());
						}
						if (($str->length() >= 2000))
						{
							$SysDocFile->SetValV2($en, $en->getPKVal(), $str);
							if ($attrs->Contains("DocLength"))
							{
								$en->SetValByKey_String_F("DocLength", $str->length());
							}
							$val = ($val . ",''");
						}
						else
						{
							$val = ((($val . ",'") . $str) . "'");
						}
					}
					else
					{
			/* match: String */
						$val = ((($val . ",'") . $en->GetValStringByKey_String($key)->replace('\'', '~')) . "'");
					}
					break;
				case $DataType->AppInt:
				case $DataType->AppBoolean:
			/* match: String */
					$val = (($val . ",") . $en->GetValIntByKey_String($key));
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$strNum = $en->GetValStringByKey_String($key);
					$strNum = $strNum->replace("�?", "");
					$strNum = $strNum->replace(",", "");
					if ($strNum->equals(""))
					{
						$strNum = "0";
					}
					$val = (($val . ",") . $strNum);
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					if (($da->indexOf("_DATE") == -1))
					{
						$val = ((($val . ",'") . $da) . "'");
					}
					else
					{
						$val = (($val . ",") . $da);
					}
					break;
				default:
					throw new \RuntimeException("@bulider insert sql error: 没有这个数据类型");
			}
		}
		$sql = (((((("INSERT INTO " . $en->getEnMap()->getPhysicsTable()) . " (") . $field->substring(1)) . " ) VALUES ( ") . $val->substring(1)) . ")");
		return $sql;
	}
	public static function InsertOFOLE ($en) // [Entity en]
	{
		$key = ""$field = ""$val = "";
		foreach ($en->getEnMap()->getAttrs() as $attr) 		{
			if (($attr->getMyFieldType() == FieldType::$RefText))
			{
				continue;
			}
			$key = $attr->getKey();
			$field = ((($field . ",[") . $attr->getField()) . "]");
			switch ($attr->getMyDataType()) {
				case $DataType->AppString:
			/* match: String */
					$val = ((($val . ", '") . $en->GetValStringByKey_String($key)) . "'");
					break;
				case $DataType->AppInt:
				case $DataType->AppBoolean:
			/* match: String */
					$val = (($val . ",") . $en->GetValIntByKey_String($key));
					break;
				case $DataType->AppFloat:
				case $DataType->AppDouble:
				case $DataType->AppMoney:
			/* match: String */
					$str = $en->GetValStringByKey_String($key);
					$str = $str->replace("�?", "");
					$str = $str->replace(",", "");
					if ($str->equals(""))
					{
						$str = "0";
					}
					$val = (($val . ",") . $str);
					break;
				case $DataType->AppDate:
				case $DataType->AppDateTime:
			/* match: String */
					$da = $en->GetValStringByKey_String($attr->getKey());
					if (($da->indexOf("_DATE") == -1))
					{
						$val = ((($val . ",'") . $da) . "'");
					}
					else
					{
						$val = (($val . ",") . $da);
					}
					break;
				default:
					throw new \RuntimeException("@bulider insert sql error: 没有这个数据类型");
			}
		}
		$sql = (((((("INSERT INTO " . $en->getEnMap()->getPhysicsTable()) . " (") . $field->substring(1)) . " ) VALUES ( ") . $val->substring(1)) . ")");
		return $sql;
	}
	public static function GetIsNullInSQL ($expression, $isNullBack) // [String expression, String isNullBack]
	{
		switch (DBAccess->getAppCenterDBType()) {
			case $MSSQL:
				return ((((" ISNULL(" . $expression) . ",") . $isNullBack) . ")");
			case $Oracle:
				return ((((" NVL(" . $expression) . ",") . $isNullBack) . ")");
			case $MySQL:
				return ((((" IFNULL(" . $expression) . ",") . $isNullBack) . ")");
			case $PostgreSQL:
				return ((((" COALESCE(" . $expression) . ",") . $isNullBack) . ")");
			default:
				throw new Exception("GetIsNullInSQL未涉及的数据库类�?");
		}
	}
}
?>
